package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Behospital;
import com.kkb.pojo.Hosregister;
import com.kkb.service.BeHospitalService;
import com.kkb.vo.BeHospitalQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
@RequestMapping("behospital")
@ResponseBody
public class BeHospitalController {
    @Resource
    private BeHospitalService beHospitalService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Behospital> queryByPage(@RequestParam(defaultValue = "1") Integer realPageNum, @RequestParam(defaultValue = "5") Integer pageSize, BeHospitalQueryVO vo) {
        PageInfo<Behospital> info = beHospitalService.queryByPage(realPageNum, pageSize, vo);
        return new ResultVO<>(info);
    }

    @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
    public ResultVO<Behospital> queryById(@PathVariable("id") Integer id) {
        Behospital behospital = beHospitalService.queryById(id);
        ResultVO<Behospital> behospitalResultVO = new ResultVO<>(behospital);
        return behospitalResultVO;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResultVO<Behospital> deleteBeHospital(@PathVariable("id") Integer id) {
        int i = beHospitalService.deleteBeHospital(id);
        if (i == 1) {
            return new ResultVO<Behospital>();
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/pay/{id}", method = RequestMethod.DELETE)
    public ResultVO<Behospital> payBeHospital(@PathVariable("id") Integer id) {
        int i = beHospitalService.payBeHospital(id);
        if (i == 1) {
            return new ResultVO<Behospital>();
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/leave/{id}", method = RequestMethod.DELETE)
    public ResultVO<Behospital> leaveHospital(@PathVariable("id") Integer id) {
        int i = beHospitalService.leaveHospital(id);
        if (i == 1) {
            return new ResultVO<Behospital>();
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/deleteAll/{ids}", method = RequestMethod.DELETE)
    public ResultVO<Behospital> deleteBeHospital(@PathVariable("ids") ArrayList<Integer> ids) {
        int i = beHospitalService.deleteAllBeHospital(ids);
        if (i >= 1) {
            return new ResultVO<Behospital>();
        }
        if (i == -1) {
            return new ResultVO<Behospital>(201, "选中的记录中存在已出院的记录");
        } else if (i == -2) {
            return new ResultVO<Behospital>(202, "选中的记录中存在已退院的记录");
        } else if (i == -3) {
            return new ResultVO<Behospital>(203, "选中的记录中存在未缴费的记录");
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/leaveAll/{ids}", method = RequestMethod.DELETE)
    public ResultVO<Behospital> leaveBeHospital(@PathVariable("ids") ArrayList<Integer> ids) {
        int i = beHospitalService.leaveAllBeHospital(ids);
        if (i >= 1) {
            return new ResultVO<Behospital>();
        }
        if (i == -1) {
            return new ResultVO<Behospital>(201, "选中的记录中存在已出院的记录");
        } else if (i == -2) {
            return new ResultVO<Behospital>(202, "选中的记录中存在已退院的记录");
        } else if (i == -3) {
            return new ResultVO<Behospital>(203, "选中的记录中存在未缴费的记录");
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public ResultVO<Behospital> updateBeHospital(@PathVariable("id") Integer id, Behospital behospital) {
        behospital.setId(id);
        System.out.println(behospital);
        int i = beHospitalService.updateBeHospital(behospital);
        if (i >= 1) {
            return new ResultVO<Behospital>();
        }
        if (i == -1) {
            return new ResultVO<Behospital>(201, "医生名与所属科室不匹配！");
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Behospital> addBeHospital(Behospital behospital) {
        int i = beHospitalService.addBeHospital(behospital);
        if (i == 1) {
            return new ResultVO<Behospital>();
        }
        return new ResultVO<Behospital>(500, "服务器内部异常，请稍后再试！");
    }
}
